<template>
	<view
		:style="'order:'+item.order+';'+item.css"
		class="list-waterfall">
		<template v-if="listStatus === 'success'">
			<view class="list-waterfall-box">
				<view
					v-for="(item, index) in list" :key="index"
					class="for"
					@click="$_to('post/detail?pid=' + item.pid)">
					<view class="for-image" v-if="item.files.images.length">
						<global-lazy-load-image :image="item.files.images[0].imageRatioUrl">
						</global-lazy-load-image>
					</view>
					<view class="for-title">
						<text>{{item.title?item.title:$_filterHtml(item.content)}}</text>
					</view>
					<view class="for-user">
						<view class="for-user-left">
							<view>
								<image :src="item.author.avatar" mode="widthFix"></image>
							</view>
							<text>{{item.isAnonymous?$t('contentAuthorAnonymous'):item.author.nickname}}</text>
						</view>
						<view class="for-user-right">
							<text>{{item.createdTimeAgo}}</text>
						</view>
					</view>
				</view>
			</view>
			<view
				class="more-content"
				v-if="item.page"
				@click="clickMoreButton">
				{{listPageText}}
			</view>
		</template>
		<template v-if="listStatus === 'loading'">
			<view class="_loading">
				<view class="skeleton-screen" v-for="itemData in item.query.pageSize?item.query.pageSize:1"></view>
			</view>
		</template>
		<template v-if="listStatus === 'empty'">
			<global-empty></global-empty>
		</template>
	</view>
</template>

<script>
	export default {
		name:"list-waterfall",
		props:{
			item:{
				type:Object,
				default:null
			}
		},
		data() {
			return {
				list:[],
				listStatus:"loading",
				listPage:2,
				listPageText: this.$t('clickToLoadMore')
			};
		},
		mounted(){
			this.getList();
		},
		methods: {
			getList(){
				this.$_request(
					"GET", "/api/fresns/v1/post/list",
					{
						...this.item.query,
						"filterType":"whitelist",
						"filterKeys":"pid,files,title,content,author,isAnonymous,createdTimeAgo",
						"filterAuthorType":"whitelist",
						"filterAuthorKeys":"avatar,nickname",
						"page":1
					},
					(res) => {
						if (res.data.data.list.length) {
							this.list = res.data.data.list;
							this.listStatus = "success";
						} else {
							this.listStatus = "empty";
						}
					}
				)
			},
			clickMoreButton(){
				if (this.listPageText == this.$t('loading')) { return }
				this.listPageText = this.$t('loading');
				this.$_request(
					"GET", "/api/fresns/v1/post/list",
					{
						...this.item.query,
						"filterType":"whitelist",
						"filterKeys":"pid,files,title,content,author,isAnonymous,createdTimeAgo",
						"filterAuthorType":"whitelist",
						"filterAuthorKeys":"avatar,nickname",
						"page": this.listPage
					},
					(res) => {
						if (res.data.data.list.length) {
							this.list = this.list.concat(res.data.data.list);
							this.listPage++
							this.listPageText = this.$t('clickToLoadMore')
						} else {
							this.listPageText = this.$t('listEmpty')
						}
					}
				)
			}
		},
	}
</script>

<style lang="scss" scoped>
.list-waterfall{
	._loading{
		background: #FFF;
		padding: 25rpx 25rpx 1rpx 25rpx;
		.skeleton-screen{
			width: 100%;
			height: 50rpx;
			margin-bottom: 25rpx;
		}
	}
	.list-waterfall-box{
		column-count: 2;
		column-gap: 25rpx;
		counter-reset: itemCounter;
		margin: 25rpx 25rpx 0 25rpx;
		.for{
			background: #FFF;
			margin-bottom: 25rpx;
			break-inside: avoid-column;
			border-radius: 10rpx;
			overflow: hidden;
			.for-image{
				width: 100%;
				image{
					display: block;
					width: 100%;
					height: auto;
				}
			}
			.for-title{
				padding: 15rpx 25rpx;
				text{
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}
			}
			.for-user{
				display: flex;
				align-items: center;
				justify-content:space-between;
				padding: 0 25rpx 25rpx 25rpx;
				.for-user-left{
					display: flex;
					align-items: center;
					line-height: 22rpx;
					view{
						width: 40rpx;
						height: 40rpx;
						overflow: hidden;
						border-radius: 100rpx;
						margin-right: 10rpx;
					}
					text{
						font-size: 22rpx;
					}
				}
				.for-user-right{
					line-height: 22rpx;
					text{
						font-size: 22rpx;
						color: $placeholder;
					}
				}
			}
		}
	}
	.more-content{
		padding: 25rpx 0;
		font-size: 22rpx;
		line-height: 22rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: $placeholder;
	}
}
</style>